<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/6/12
 * Time: 上午9:32
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AdminRole extends Model
{
    use SoftDeletes;

    protected $table = 'admin_role';

    public $casts = [
        'resource' => 'json',
    ];

    public static $whitelist = [
        'home',
        'resource'
    ];

    public function getResourceNames()
    {
        $resource = config('resource.admin');
        $temp     = [];
        foreach ($resource as $key => $item) {
            if (in_array($key, $this->resource)) {
                $temp[] = $item['name'];
                continue;
            }
            if ($item['children']) {
                foreach ($item['children'] as $k => $child) {
                    if (in_array($k, $this->resource)) {
                        $temp[] = $child['name'];
                        continue;
                    }
                }
            }
        }
        return $temp;
    }

    public function getResourceRoutes()
    {
        $resource = config('resource.admin');
        $temp     = [];
        foreach ($resource as $key => $item) {
            if (in_array($key, $this->resource)) {
                $temp = array_merge($temp, $item['routes']);
                continue;
            }
            if ($item['children']) {
                foreach ($item['children'] as $k => $child) {
                    if (in_array($k, $this->resource)) {
                        $temp = array_merge($temp, $child['routes']);
                        continue;
                    }
                }
            }
        }
        return $temp;
    }
}